const cuttent = require('../models/Positions')
const delivers = require('../models/delivers')
const R = require('../utils/R')
class CityListsCtl {
    async find(ctx) {
        // 每页条数
        const perPage = parseInt(ctx.query.perPage) || 10
        // 当前页
        const page = parseInt(ctx.query.page) || 1
        // 跳过条数
        const skip = (page - 1) * perPage
        // 分页 模糊搜索
        const title = new RegExp(ctx.query.title)
        const { city_list_id , job_function_id  } = ctx.query
        const query = {
            title,
            // city_list: {
            //     [city_list_id?_id:'']: city_list_id
            // },
            // job_function: {
            //     [job_function_id?_id:'']: job_function_id
            // }
        }
        // if (title) {
        //     query.title = { $regex: /title/ }
        // }
        if (city_list_id) {
            query.city_list = {
                _id: city_list_id
            }
        }
        if (job_function_id) {
            query.job_function = {
                _id: job_function_id
            }
        }
       

        const populate ='city_list;job_type;recruit_type;job_project;job_subject;job_function'
        const populateStr = populate.split(';').filter(f => f).map((f) => f).join(' ')
        const data = await cuttent.find(query).limit(perPage).skip(skip).sort({ createdAt: -1 }).populate(populateStr)
        // 总条数
        const total = await cuttent.find(query).countDocuments()
        // 向上取整 一共有多少页
        const totalPage = Math.ceil(total / perPage)
        ctx.body = R.ok().data({
            data,
            page,
            perPage,
            total,
            totalPage,
        })
    }
    async findById(ctx) {
        // 职位详情
        const populate ='city_list;job_type;recruit_type;job_project;job_subject;job_function'
        const populateStr = populate.split(';').filter(f => f).map((f) => f).join(' ')
        const data = await cuttent.findById(ctx.params.id).populate(populateStr)
            .populate('commentator replyTo')
      
        // 是否投递该职位
        const deliverList = await delivers.find({
            user_id: ctx.state.user._id,
            positions: ctx.params.id
        })
        let isdeliver = false
        if (deliverList) {
            isdeliver = true
        } else {
            isdeliver = false
        }

    
        ctx.body = R.ok().data({
            ...data._doc,
            isdeliver
        })
    }
    async create(ctx) {
        ctx.verifyParams({
            title: { type: 'string', required: true },
            description: { type: 'string', required: true },
            requirement: { type: 'string', required: true },
            city_list: {  type: 'array', itemType: 'string', required: true },
            job_type: {  type: 'array', itemType: 'string', required: true },
            recruit_type: {  type: 'array', itemType: 'string', required: true },
            job_function: { type: 'array', itemType: 'string', required: true },
        })
        const { title } = ctx.request.body
        let data = null
        data = await cuttent.findOne({ title })
        if (data) {
            ctx.body = R.setResult({ status: 10000, message: '数据重复'}).data(null)
            return
        }
        data = await new cuttent(ctx.request.body).save()
        ctx.body = R.ok().data(data)
    }

    async update(ctx) {
        ctx.verifyParams({
            name: { type: 'string', required: true },
        })
        var data = await cuttent.findByIdAndUpdate(ctx.params.id, ctx.request.body, {
            new: true
        })
        if (!data) {
            ctx.body = R.setResult({ status: 10001, message: '数据不存在'}).data(null)
            return
        }
        ctx.body = R.ok().data(data)
    }
}


module.exports = new CityListsCtl()